package com.texas_android.app.View;

import android.content.Intent;
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.texas_android.app.Appconfig.AppConfig;
import com.texas_android.app.Appconfig.AppContext;
import com.texas_android.app.Appconfig.MyApplication;
import com.texas_android.app.R;
import com.texas_android.app.http.Api;
import com.texas_android.app.http.ResponseHandler;
import com.texas_android.app.http.bean.SearchResultBean;
import com.texas_android.app.http.bean.company.CompanyBean;
import com.texas_android.app.http.bean.company.PracticalLeaderBean;
import com.texas_android.app.http.bean.perssion.PerssionBean;
import com.texas_android.app.http.bean.perssion.PerssionListResultBean;
import com.texas_android.app.utils.AppOperator;
import com.texas_android.app.utils.DialogHelper;
import com.texas_android.app.utils.Logger;
import com.texas_android.app.utils.dateBase.DataBaseHelper;
import com.universal_library.activity.BaseActivity;
import com.universal_library.weight.AppToast;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import cz.msebera.android.httpclient.Header;

/**
 * 单位信息页面
 */
public class CompanyInfoActivity extends BaseActivity implements View.OnClickListener {
    @Bind(R.id.iv_turn_bakc)
    ImageView iv_turn_bakc;

    @Bind(R.id.iv_home)
    ImageView iv_home;

    @Bind(R.id.tv_tile)
    TextView tv_title;
    /**
     * 表格第一行数据   单位的基本信息
     */
    //机构名称
    @Bind(R.id.tv_companyName)
    TextView tv_companyName;
    //简称
    @Bind(R.id.tv_nickName)
    TextView tv_nickName;
    //机构性质
    @Bind(R.id.tv_companyType)
    TextView tv_companyType;
    //单位级别
    @Bind(R.id.tv_companyLeve)
    TextView tv_companyLeve;


    /**
     * 表格第二行需要填入的数据  编制人数
     *
     * @return
     */
    //行政的应配人数
    @Bind(R.id.tv_hr_num)
    TextView tv_hr_num;
    //事业参公人数
    @Bind(R.id.tv_publicPer_num)
    TextView tv_publicPer_num;
    //事业非参公应配人数
    @Bind(R.id.tv_privatePerNum)
    TextView tv_privatePerNum;

    /**
     * 表格第三行需要填入的数据  编制实际人数
     *
     * @return
     */
    //行政实际的人数
    @Bind(R.id.tv_hr_practivalNum)
    TextView tv_hr_practivalNum;

    //事业参公的实际人数
    @Bind(R.id.tv_practivalPublicPerNum)
    TextView tv_practivalPublicPerNum;

    //事业非参公的实际人数
    @Bind(R.id.tv_practicalPrivatePerNum)
    TextView tv_practicalPrivatePerNum;

    /**
     * 应配领导人数
     *
     * @return
     */
    //正职领导人数
    @Bind(R.id.tv_leader_num1)
    TextView tv_leader_num1;

    //副职领导人数
    @Bind(R.id.tv_leader_num2)
    TextView tv_leader_num2;

    //正职非领导人数
    @Bind(R.id.tv_leader_num3)
    TextView tv_leader_num3;
    //副职非领导
    @Bind(R.id.tv_leader_num4)
    TextView tv_leader_num4;

    /**
     * 领导的实际人数
     *
     * @return
     */
    //正职领导的实际人数
    @Bind(R.id.tv_leader_practical_num1)
    TextView tv_leader_practical_num1;

    //副职领导的实际人数
    @Bind(R.id.tv_leader_practical_num2)
    TextView tv_leader_practical_num2;

    //正职非领导的实际人数
    @Bind(R.id.tv_leader_practical_num3)
    TextView tv_leader_practical_num3;

    //副职非领导的实际人数
    @Bind(R.id.tv_leader_practical_num4)
    TextView tv_leader_practical_num4;
    private DialogHelper.ZXQbListDialog dialog;
    /**
     * 任职要素
     *
     * @return
     */
    @Bind(R.id.tv_professional_1)
    TextView tv_professional_1;

    @Bind(R.id.tv_professional_2)
    TextView tv_professional_2;

    @Bind(R.id.tv_professional_3)
    TextView tv_professional_3;

    @Bind(R.id.tv_professional_4)
    TextView tv_professional_4;

    //单位职能
    @Bind(R.id.tv_functional)
    TextView tv_functional;
    CompanyBean companyBean = null;
    private DataBaseHelper dataBaseHelper;

    @Override
    protected int getLayoutId() {
        if (getIntent() != null && getIntent().getSerializableExtra("companyBean") != null)
            companyBean = (CompanyBean) getIntent().getSerializableExtra("companyBean");
        return R.layout.activity_company_info_new;
    }

    @Override
    protected void initView() {
        AppContext.addActivity(this);
        tv_title.setText("机构基本信息表");
        tv_professional_1.setMovementMethod(ScrollingMovementMethod.getInstance());
        tv_professional_2.setMovementMethod(ScrollingMovementMethod.getInstance());
        tv_professional_3.setMovementMethod(ScrollingMovementMethod.getInstance());
        tv_professional_4.setMovementMethod(ScrollingMovementMethod.getInstance());
        tv_functional.setMovementMethod(ScrollingMovementMethod.getInstance());
        try {
            dataBaseHelper = new DataBaseHelper(this);
            dataBaseHelper.openDataBase();
            dataBaseHelper.searchNubHr(companyBean.getCompanyId(), companyBean);
            dataBaseHelper.searchSybCg(companyBean.getCompanyId(), companyBean);
            dataBaseHelper.searchShiyebianfgong(companyBean.getCompanyId(), companyBean);
            PracticalLeaderBean practicalLeaderBean = new PracticalLeaderBean();
            companyBean.setPracticalLeaderList(practicalLeaderBean);
            dataBaseHelper.searchZZLeader(companyBean.getCompanyId(), companyBean);
            dataBaseHelper.searchFZLeader(companyBean.getCompanyId(), companyBean);
            dataBaseHelper.searchZZFLeader(companyBean.getCompanyId(), companyBean);
            dataBaseHelper.searchFZFLeader(companyBean.getCompanyId(), companyBean);
            dataBaseHelper.searchRenzhiyaoshu(companyBean.getCompanyId(), companyBean);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
//            updateView();
        }
    }

    @Override
    protected void initData() {
        if (companyBean != null) {
            tv_companyName.setText(companyBean.getCompanyName() + "");

            tv_nickName.setText(companyBean.getCompanyShortName() + "");
            if (companyBean.getCompanyType().equals("1")) {
                tv_companyType.setText("行政机关");
            } else if (companyBean.getCompanyType().equals("2")) {
                tv_companyType.setText("参公群体");

            } else if (companyBean.getCompanyType().equals("3")) {
                tv_companyType.setText("参公事业");
            } else if (companyBean.getCompanyType().equals("4")) {
                tv_companyType.setText("事业");
            } else {
                tv_companyType.setText(companyBean.getCompanyType() + "");
            }
            //等级
            if (companyBean.getCompanyRank().equals("1")) {
                tv_companyLeve.setText("正县级");
            } else if (companyBean.getCompanyRank().equals("2")) {
                tv_companyLeve.setText("副县级");
            } else if (companyBean.getCompanyRank().equals("3")) {
                tv_companyLeve.setText("正科级");
            } else if (companyBean.getCompanyRank().equals("4")) {
                tv_companyLeve.setText("副科级");
            } else if (companyBean.getCompanyRank().equals("5")) {
                tv_companyLeve.setText("股级");
            } else {
                tv_companyLeve.setText("" + companyBean.getCompanyRank());

            }

            //hr的人数 和实际人数
            tv_hr_num.setText(companyBean.getHrPerNum() + "");
            tv_hr_practivalNum.setText(companyBean.getHrPracticalNum() + "");
            //事业编制
            tv_publicPer_num.setText(companyBean.getPublicPerNum() + "");
            tv_practivalPublicPerNum.setText(companyBean.getPublicPerPracticalNum() + "");

            tv_privatePerNum.setText(companyBean.getPrivatePerNum() + "");
            tv_practicalPrivatePerNum.setText(companyBean.getPrivatePerPracticalNum() + "");

            tv_leader_num1.setText(companyBean.getConfigLeaderList().getZzld() + "");
            tv_leader_num2.setText(companyBean.getConfigLeaderList().getFzld() + "");
            tv_leader_num3.setText(companyBean.getConfigLeaderList().getZzld() + "");
            tv_leader_num4.setText(companyBean.getConfigLeaderList().getFzfld() + "");
            if (companyBean.getPracticalLeaderList() != null)
                if (!TextUtils.isEmpty(companyBean.getPracticalLeaderList().getSpzzld() + "")) {
                    tv_leader_practical_num1.setText(companyBean.getPracticalLeaderList().getSpzzld() + "");
                    tv_leader_practical_num2.setText(companyBean.getPracticalLeaderList().getSpfzld() + "");
                    tv_leader_practical_num3.setText(companyBean.getPracticalLeaderList().getSpzzfld() + "");
                    tv_leader_practical_num4.setText(companyBean.getPracticalLeaderList().getSpfzfld() + "");
                }
            if (companyBean.getProfessionalInfoList() != null) {
                tv_professional_1.setText(companyBean.getProfessionalInfoList().getYszzld());
                Logger.log("要输："+companyBean.getProfessionalInfoList().getYszzld());
                tv_professional_2.setText(companyBean.getProfessionalInfoList().getYsfzld());
                tv_professional_3.setText(companyBean.getProfessionalInfoList().getYszzfld());
                tv_professional_4.setText(companyBean.getProfessionalInfoList().getYsfzfld());
            }
        }
    }

    @OnClick({R.id.iv_home, R.id.iv_turn_bakc})
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_home:
                Intent intent = new Intent(this, MainActivity.class);
                startActivity(intent);
                finish();
                break;
            case R.id.iv_turn_bakc:
                finish();
                break;
        }
    }

    public int voiceSearchKey(String searchText) {
        Log.i("公司页面查询结果：", searchText);
        if (searchText.contains("返回桌面") || searchText.contains("返回系统")) {
            AppContext.closeActivityAll();
            return 0;
        }
        if (!TextUtils.isEmpty(searchText) && searchText.length() >= 2) {

            earchInfo(searchText);
        }
        return 0;
    }

    public void earchInfo(String search) {
        Api.searchInfo(searchHandler, MyApplication.aCache.getAsString(AppConfig.APP_SERVER_TOKEN), search);
    }

    private ResponseHandler searchHandler = new ResponseHandler(this, false, "查询") {
        @Override
        public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
            super.onFailure(statusCode, headers, responseString, throwable);
        }

        @Override
        public void onSuccess(int statusCode, Header[] headers, final String responseString) {
            super.onSuccess(statusCode, headers, responseString);
            if (baseBean.getStatus().equals("0")) {
                if (baseBean.getType() == 1) {
                    SearchResultBean resultBean = AppOperator.createGson().fromJson(responseString, new TypeToken<SearchResultBean>() {
                    }.getType());
                    CompanyBean companyBean = resultBean.getCompanyBean().get(0);
                    //查询结果部门
                    Intent intent = new Intent(CompanyInfoActivity.this, CompanyInfoActivity.class);
                    intent.putExtra("companyBean", companyBean);
                    overridePendingTransition(R.anim.slide_in_top, R.anim.slide_in_top);
                    startActivity(intent);
                } else {
                    final PerssionListResultBean perssionListResultBeanr = AppOperator.createGson().fromJson(responseString, new TypeToken<PerssionListResultBean>() {
                    }.getType());
                    if (perssionListResultBeanr.getPerssionBean().size() > 1) {
                        List<String> mdata = new ArrayList<>();
                        List<PerssionBean> listPersion = perssionListResultBeanr.getPerssionBean();

                        for (PerssionBean bean : listPersion) {
                            String showInfo = "<font color='#404040'> 姓名：</font>" + bean.getName()
                                    + "<font color='#404040'>  年龄：</font>" + bean.getAge()
                                    + "<font color='#404040'>  部门：</font>" + bean.getDeptFullName();
                            mdata.add(showInfo);
                        }
                        if (dialog != null && dialog.isShowing()) {
                            return;
                        }
                        dialog = DialogHelper.openZxListDialog(CompanyInfoActivity.this, mdata, "请确认您要查询的信息", 06, new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                                dialog.dismiss();
                                PerssionBean perssionBean = perssionListResultBeanr.getPerssionBean().get(i);
                                //结果查询的人
                                Intent intent = new Intent(CompanyInfoActivity.this, PerssonInfoActivity.class);
                                intent.putExtra("perBean", perssionBean);
                                overridePendingTransition(R.anim.slide_in_top, R.anim.slide_in_top);
                                startActivity(intent);
                            }
                        }, new DialogHelper.OnItemSelectListenter() {
                            @Override
                            public void mySelect(int dex) {
                                if (dex <= 6 && dex < perssionListResultBeanr.getPerssionBean().size()) {
                                    try {
                                        dialog.dismiss();
                                        PerssionBean perssionBean = perssionListResultBeanr.getPerssionBean().get(dex);
                                        //结果查询的人
                                        Intent intent = new Intent(CompanyInfoActivity.this, PerssonInfoActivity.class);
                                        intent.putExtra("perBean", perssionBean);
                                        overridePendingTransition(R.anim.slide_in_top, R.anim.slide_in_top);
                                        startActivity(intent);
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                        AppToast.showToast(CompanyInfoActivity.this, "您说的我不明白", "请手动选择");
                                    }

                                } else {
                                    AppToast.showToast(CompanyInfoActivity.this, "您说的我不明白", "请手动选择");
                                }

                            }
                        });
                    }
                }
            } else {
                AppToast.showToast(CompanyInfoActivity.this, "", baseBean.getStatusDesc());
            }
        }
    };
}
